package com.example.casper.Experiment2024;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class TourMemoryFragment extends Fragment {
    private final String[] tourtabStrings = {"图书","搜素","地图"};

    public TourMemoryFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootview = inflater.inflate(R.layout.fragment_tour_memory, container, false);
        ViewPager2 viewPager = rootview.findViewById(R.id.tour_view_pager);
        TabLayout tabLayout = rootview.findViewById(R.id.tour_tab_layout);
        TourFragmentAdapter fragmentAdapter = new TourFragmentAdapter(getChildFragmentManager(), getLifecycle());
        viewPager.setAdapter(fragmentAdapter);
        new TabLayoutMediator(tabLayout, viewPager,
                (tab, position) -> tab.setText(tourtabStrings[position])
        ).attach();
        return rootview;
    }
    public static class TourFragmentAdapter extends FragmentStateAdapter {
        private static final int NUM_TABS = 3;
        public TourFragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
            super(fragmentManager, lifecycle);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            // 根据位置返回对应的Fragment实例
            switch (position) {
                case 0:
                    return BookListFragment.newInstance();
                case 1:
                    return WebViewFragment.newInstance("https://baidu.com");
                case 2:
                    return new TencentMapFragment();
                default:
                    return new BookListFragment();
            }
        }
        @Override
        public int getItemCount() {
            return NUM_TABS;
        }
    }
}